import 'package:collection/collection.dart';
import 'package:excel/excel.dart';
import 'package:flutter/cupertino.dart';

List<int>? exportGoodsToExcel(List<GoodsRow> data){
  var excel = Excel.createExcel();
  var sheet = excel[excel.getDefaultSheet()!];
  sheet.insertRow(0);
  ["名称", "条码", "零售价", "库存"].forEachIndexed((index, element) {
    sheet
        .cell(CellIndex.indexByColumnRow(
        rowIndex: 0, columnIndex: index))
        .value = element;
  });
  data.forEachIndexed((index, element) {
    var rowIndex = index + 1;
    sheet
        .cell(CellIndex.indexByColumnRow(
        rowIndex: rowIndex, columnIndex: 0))
        .value = element.name;
    sheet
        .cell(CellIndex.indexByColumnRow(
        rowIndex: rowIndex, columnIndex: 1))
        .value = element.barcode;
    sheet
        .cell(CellIndex.indexByColumnRow(
        rowIndex: rowIndex, columnIndex: 2))
        .value = element.price;
    sheet
        .cell(CellIndex.indexByColumnRow(
        rowIndex: rowIndex, columnIndex: 3))
        .value = element.amount;
  });
  return excel.save();
}

class GoodsRow{
  final String name;
  final String barcode;
  final double price;
  final int? amount;
  GoodsRow({required this.name, required this.barcode, required this.price, this.amount});
}